#include <string.h>
#include <wget.h>

void *http_request(const char *method, const char *uri,
    const char *post_data, size_t data_len, void *user_data)
{
  void *res = NULL;
  wget_http_connection *conn = NULL;
  wget_http_request *req;
  wget_http_response *resp;
  wget_iri *iri;

  //printf("[DEBUG] [L%d f-%s] method:%s url:%s\n", __LINE__, __func__, method, uri);
  /*
  wget_global_init(
     // WGET_DEBUG_STREAM, stderr, WGET_ERROR_STREAM, stderr,
      //WGET_INFO_STREAM, stdout,
      0);
      */

  iri = wget_iri_parse(uri, NULL);
  req = wget_http_create_request(iri, method);
  wget_http_add_header(req, "Connection", "keepalive");
  if (post_data) {
   // printf("[DEBUG] [L%d f-%s] body:%s\n", __LINE__, __func__, post_data);
    //wget_http_request_set_body(req, "application/x-www-form-urlencoded",
    wget_http_request_set_body(req, "application/json",
        wget_memdup(post_data, data_len), data_len);
  }
  wget_http_open(&conn, iri);
  if (conn) {
    if (!wget_http_send_request(conn, req)) {
      resp = wget_http_get_response(conn);
      if (resp && (resp->body != NULL)) {
        res = calloc(1, resp->body->length+2);
        memcpy(res, resp->body->data, resp->body->length);
        wget_http_free_response(&resp);
      }
    }
  }
  wget_http_free_request(&req);
  wget_iri_free(&iri);
  wget_http_close(&conn);
//  wget_global_deinit();
  //printf("[DEBUG]L%d f-%s res:%s\n", __LINE__, __func__, (char *)res);

  return res;
}
void *http_request_digest(const char *method, const char *uri,
    const char *post_data, size_t data_len, size_t *size_p,
    const char *username, const char *password)
{
  void *res = NULL;
  wget_http_connection *conn = NULL;
  wget_http_request *req;
  wget_http_response *resp;
  wget_iri *iri;

  //printf("[DEBUG]L%d f-%s method:%s url:%s\n", __LINE__, __func__, method, uri);
  wget_global_init(
     // WGET_DEBUG_STREAM, stderr, WGET_ERROR_STREAM, stderr,
      //WGET_INFO_STREAM, stdout,
      0);
  wget_tcp_set_timeout(NULL, 5000);
  iri = wget_iri_parse(uri, NULL);
  req = wget_http_create_request(iri, method);
  wget_http_add_header(req, "Connection", "keep-alive");
  wget_http_add_header(req, "Accept", "*/*");
  wget_http_add_header(req, "Accept-Encoding", "gzip");
  /*
  wget_http_challenge *challenge = NULL;
  challenge = wget_calloc(1, sizeof(wget_http_challenge));
  wget_http_parse_challenge("Digest realm=\"test realm\"", challenge);
  wget_http_add_credentials(req, challenge, username, password, 0);
  */
  if (post_data)
    //wget_http_request_set_body(req, "application/x-www-form-urlencoded",
    wget_http_request_set_body(req, "application/json",
        wget_memdup(post_data, data_len), data_len);
  wget_http_open(&conn, iri);
  if (conn) {
    if (!wget_http_send_request(conn, req)) {
      resp = wget_http_get_response(conn);
      if (resp) {
        wget_http_add_credentials(req, wget_vector_get(resp->challenges, 0),
            username, password, 0);
        wget_http_free_response(&resp);
        if (!wget_http_send_request(conn, req)) {
          resp = wget_http_get_response(conn);
          if (resp) {
            res = calloc(1, resp->body->length);
            memcpy(res, resp->body->data, resp->body->length);
            *size_p = resp->body->length;
            wget_http_free_response(&resp);
          }
        }
      }
    }
  }
  //wget_http_free_challenge(challenge);
  wget_http_free_request(&req);
  wget_iri_free(&iri);
  wget_http_close(&conn);
  wget_global_deinit();
  //printf("[DEBUG]L%d f-%s res:%s\n", __LINE__, __func__, (char *)res);

  return res;
}
